/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.poc.base.node;

import com.herenit.beijing.dh2011.security.entity.SecurityMenu;
import java.awt.Image;
import javax.swing.Action;
import javax.swing.ImageIcon;
import org.openide.actions.ActionManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Obama
 */
public class MenuNode extends AbstractNode {

    private SecurityMenu menu;

    @Override
    public Action getPreferredAction() {
        if (menu.getOriginalFile() == null) {
            return super.getPreferredAction();
        }
        Action a = FileUtil.getConfigObject(menu.getOriginalFile(), Action.class);
        if (a != null) {
            return a;
        } else {
            return super.getPreferredAction();
        }
//        String originalFile = menu.getOriginalFile();
//        String path = originalFile.substring(0, originalFile.lastIndexOf("/"));
//        String clazz = originalFile.substring(originalFile.lastIndexOf("/") + 1, originalFile.lastIndexOf("."));
//        String ext = originalFile.substring(originalFile.lastIndexOf(".")+1);
//        FileObject fo = FileUtil.getConfigFile(path);
//        if (menu.isLeaf()) {
//            for (Action action : Lookups.forPath(path).lookupAll(Action.class)) {
//
//                System.out.println(ext);
//                System.out.println(path);
//                System.out.println(clazz);
//                System.out.println(action.getClass().getName());
//
//                FileObject target = fo.getFileObject(clazz, ext);
//                    
//                
//                System.out.println(target.getPath());
//                if (action.getClass().getName().equals(clazz.replace("-", "."))) {
//                    System.out.println(clazz.replace("-", "."));
//                    return action;
//                }
//            }
//            return super.getPreferredAction();
//        } else {
//            return super.getPreferredAction();
//        }
    }

    public MenuNode(SecurityMenu menu, Children children) {
        super(children);
        this.menu = menu;
        setDisplayName(menu.getDisplayName());
        String iconBase = menu.getIcon();
        if (iconBase != null) {
            setIconBaseWithExtension(iconBase);
        }
    }
}
